package com.baidu.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class D03_InputStreamDemo2 {
    public static void main(String[] args) throws Exception {
        //1.创建字节输入流管道
        InputStream inputStream = new FileInputStream("src/003.txt");

        //2.一次性读取多个字节 使用字节数组作为缓冲
        byte[] bytes = new byte[3];

        //3.从管道中获取字节 返回获取的字节数
        int byteCnt = inputStream.read(bytes);

        //4.读取最后一批字节时 如果小于缓冲区大小 可能尾部字节还是上一批的字节  故转换时限制字节长度
        //String str = new String(bytes, 0, byteCnt);

        //5.一次性读取所有的字节
        File file = new File("src/003.txt");
        FileInputStream is = new FileInputStream(file);
        //5.1.把缓冲定义为文件大小
        byte[] bytesAll = new byte[(int) file.length()];
        //5.2一次性获取
        int read = is.read(bytesAll);
        String string = new String(bytesAll);
        System.out.println(string);
    }
}
